﻿package com.wandian.util
{   
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;

    /**
     * 全局定时器,每秒都会去回调注册的函数
     * @author long
	 * @Create  2012-10-22
	 * @QQ 108232706
	 * @version 1.0
     */
    public class GolbalTimer extends Object
    {
        /**
         * 系统定时器
         * @default 
         */
        private var _timer:Timer;
        /**
         * 回调字典，key=value=fun
         * @default 
         */
        private var dict:Dictionary;
        private static var instance:GolbalTimer;

        public function GolbalTimer()
        {
            this.dict = new Dictionary();
            this._timer = new Timer(1000);
            this._timer.addEventListener(TimerEvent.TIMER, this.timerHandler);
            this._timer.start();
            return;
        }

        private function timerHandler(event:TimerEvent) : void
        {
            var funName:* = undefined;
            for (funName in this.dict)
            {
				this.dict[funName]();
//                var _loc_5:* = this.dict;
//                _loc_5.this.dict[funName]();
            }
            return;
        }

        public function addFun(fun:Function) : void
        {
            if (this.dict[fun])
            {
                return;
            }
            this.dict[fun] = fun;
            return;
        }

        public function removeFun(fun:Function) : void
        {
            if (this.dict[fun])
            {
                delete this.dict[fun];
            }
            return;
        }

        public static function getInstance() : GolbalTimer
        {
            if (instance == null)
            {
                instance = new GolbalTimer;
            }
            return instance;
        }

    }
}
